package cate.game.role.task;

import cate.common.table.d.GDFunc;
import cate.common.table.task.row.TaskWeeklyBoxRow;
import cate.common.table.task.row.TaskWeeklyRow;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.role.RoleDependent;
import cate.game.role.Role;
import cate.game.role.task.msg.TaskWeeklyActiveResp;
import cate.game.role.task.msg.TaskWeeklyBoxResp;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass(value = "主角的每周任务数据")
public class TaskWeekly extends RoleDependent {
    @NoteField(value = "活跃度")
    public long active;

    @NoteField(value = "宝箱领取情况", detail = "Map<宝箱ID, 是否领取>")
    public Map<Integer, Boolean> rewardGot;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (rewardGot == null) {
            rewardGot = new HashMap<>();
        }
    }

    @Override
    public void onEnter() {
        TaskGroup group = role.getTask().getGroup(GDFunc.TASK_WEEKLY);
        List<Integer> taskIds = new ArrayList<>();
        for (TaskWeeklyRow dTpl : role.getGame().table.task.weekly.getList()) {
            taskIds.add(dTpl.taskId);
        }
        group.addItemListNoRepeat(taskIds);
    }

    @Override
    public void onWeekSpan(boolean silence) {
        active = 0;
        rewardGot.clear();
        TaskGroup group = role.getTask().getGroup(GDFunc.TASK_WEEKLY);
        group.clear();
        List<Integer> taskIds = new ArrayList<>();
        for (TaskWeeklyRow dTpl : role.getGame().table.task.weekly.getList()) {
            taskIds.add(dTpl.taskId);
        }
        group.addItemListNoRepeat(taskIds);
        this.noticeActiveUpdate();
        this.noticeBoxUpdate();
    }

    /**
     * 增加活跃度
     */
    public void addActive(long value) {
        active += value;
        noticeActiveUpdate();
        role.getHistory().action.weeklyActiveValue(value);
    }

    /**
     * 获取活跃度
     */
    public long getActive() {
        return active;
    }

    public void noticeActiveUpdate() {
        role.sendNow(new TaskWeeklyActiveResp(role));
    }

    public void noticeBoxUpdate() {
        role.sendNow(new TaskWeeklyBoxResp(role));
    }

    /**
     * 领取宝箱
     *
     * @param boxId 宝箱ID
     */
    public GameResult<Void> getBox(int boxId) {
        GameResult<Void> r = new GameResult<>();
        TaskWeeklyBoxRow tpl = role.getGame().table.task.weeklyBox.get(boxId);
        if (tpl == null) {
            return r.fail("没有这种宝箱");
        }
        Boolean got = this.rewardGot.get(boxId);
        if (got != null && got) {
            return r.fail("宝箱已领取过了");
        }
        if (getActive() < tpl.active) {
            return r.fail("活跃度不足");
        }
        MixRes reward = new MixRes(tpl.rewardStr);
        reward.add(role, true, 0);
        role.getGame().notice.dialogReward(role, reward);
        this.rewardGot.put(boxId, true);
        this.noticeBoxUpdate();
        role.getHistory().action.weeklyRewardTimes();
        return r.success();
    }
}
